package com.duk.main;

import java.util.HashMap;

import com.duk.util.DataBaseUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityShowDescription extends Activity {
	WebView webView;
	ImageView favorite;
	ImageView share;
	String title = null;
	String pubdate = null;
	String description = null;
	String image_path = null;
	String url = null;
	String content = null;	
	String news_type = null;
	HashMap<String,String> hm = new HashMap<String,String>();
	Handler handler;
	
	static final String mimeType = "text/html";
	static final String encoding = "utf-8";
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.showdescription);
		goBack();
		Intent startingIntent = getIntent();
		if (startingIntent != null) {
			Bundle bundle = startingIntent
					.getBundleExtra("android.intent.extra.rssItem");
			if (bundle == null) {
				content = "暂无新闻内容！";
			} else {
				image_path = bundle.getString("image_path");
				url = bundle.getString("link");
				title = bundle.getString("title") + "\r\n";
				pubdate= bundle.getString("pubdate") + "\r\n";
				description = bundle.getString("description").replace('\n', ' ');
				content = bundle.getString("content").replace('\n', ' ');
				
				hm.put("news_id", url);
			    hm.put("title", bundle.getString("title"));
			    hm.put("content", bundle.getString("content"));
			    hm.put("url", url);
			    hm.put("image_path", image_path);
//			    hm.put("news_type", bundle.getString("news_type"));
			    hm.put("news_type", "2");
			    hm.put("webdata", bundle.getString("description"));
			    hm.put("pubdate", bundle.getString("pubdate"));
				
			}
			
			handler = new Handler(){
				@Override
				public void handleMessage(Message msg) {
					if(msg.what == 1){
						favorite.setBackgroundResource(R.drawable.reduce);
					}
				}
			};	
		} else {
			content = "暂无新闻内容！";
		}
		
		new isFavorate().start();
		
		TextView textView = (TextView) findViewById(R.id.content);
		textView.setText(content);
		webView = (WebView) findViewById(R.id.webView);
		WebSettings localWebSettings = webView.getSettings();
		localWebSettings.setCacheMode(1);
	    localWebSettings.setJavaScriptEnabled(true);
	    localWebSettings.setBuiltInZoomControls(true);
	    localWebSettings.setSupportZoom(true);
	    localWebSettings.setDefaultTextEncodingName("GBK");
	    webView.setVisibility(0);
	     
	    // 构造HTML:  
	    StringBuilder html = new StringBuilder(content.length()+200);  
	    html.append("<html><head><title>")  
	        .append("dfd")  
	        .append("</title>\n")  	        
	        .append("</head><body>")
	        .append("<h2 style=\"color:#497FA1\">"+title+"</h2>"+pubdate+ " 生活新报  <br />")
	        .append("<img src=\""+image_path+"\"><h5></h5>" + description+ "<h5></h5>")
	        .append(content)
	        .append("</body></html>");  
	     WebView web = (WebView) this.findViewById(R.id.webView);  
	     web.loadDataWithBaseURL(  
	              url,  
	              html.toString(),  
	              "text/html",  
	              "UTF-8",  
	              null  
	     );  
	    
	    //收藏事件
		favorite = (ImageView)findViewById(R.id.favorite);
		favorite.setOnClickListener(new OnClickListener() {
			@Override
		    public void onClick(View v) {
				addDelFavorite();
		    }
		 });   
		     
		//分享链接事件  
		share = (ImageView)findViewById(R.id.share);
		share.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("sss");
				shareDialog(ActivityShowDescription.this).show();
			}
		});
	/*Button backbutton = (Button) findViewById(R.id.back);

		backbutton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/
	}
	 
	private void addDelFavorite()
	{
		DataBaseUtil localDataBaseUtil1 = new DataBaseUtil(this);
		localDataBaseUtil1.open();
	    if (localDataBaseUtil1.insertData(hm)){
	      this.favorite.setBackgroundResource(R.drawable.reduce);
	      favoriteDialog(this, getString(R.string.save_favorite_success)).show();
	    } else {
	      localDataBaseUtil1.deleteByNewsId(url);
	      this.favorite.setBackgroundResource(R.drawable.enlarge_02);
	      favoriteDialog(this, getString(R.string.save_favorite_Exist)).show();
	    }	
	}
	
	//新闻收藏
	private Dialog favoriteDialog(Context context,String message)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    builder.setTitle(R.string.save_favorite);
	    builder.setMessage(message);
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
	    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
	    return builder.create();   
	 } 	
	
	
	//分享链接
	private Dialog shareDialog(Context context)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    builder.setTitle(R.string.share_noti);
	    builder.setMessage(R.string.share_message);
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				shareByEmail(url);
			}
		});
	    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
	    return builder.create();   
	 } 
	
	 private void shareByEmail(String paramString)
	 {
	    Intent shareIntent1 = new Intent("android.intent.action.SEND");
	    shareIntent1.setType("text/plain");
	    String str1 = getString(R.string.visit_bjnews);
	    shareIntent1.putExtra("android.intent.extra.SUBJECT", str1);
	    String str2 = String.valueOf(getString(R.string.visit_bjnews_content));
	    String str3 = str2 + paramString;
	    shareIntent1.putExtra("android.intent.extra.TEXT", str3);
	    startActivity(shareIntent1);
	  }
	
	
	 //返回
	 private void goBack() {
		 TextView backtv = (TextView) findViewById(R.id.back_iamge1);
		 
		 backtv.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityShowDescription.this.setResult(RESULT_OK,getIntent());
				ActivityShowDescription.this.finish();
				
			}
		});
	 }
	 //是否收藏
	class isFavorate extends Thread
	{
		public void run()
		{
			DataBaseUtil localDataBaseUtil1 = new DataBaseUtil(ActivityShowDescription.this);
			Message msg = new Message();
			localDataBaseUtil1.open();
			if (localDataBaseUtil1.checkExist(url)){
				msg.what = 1;
			} else {
				msg.what = 0;
			}
			handler.sendMessage(msg);
		}
	}
	
	
	
}
